layui.use(['form','layer','table','laytpl','tree'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        tree = layui.tree;

    //用户列表
    var tableIns = table.render({
        elem: '#studentList',
        url : '/studentList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "studentListTable",
        toolbar: true,
        cols : [[
        {type: "checkbox", fixed:"left", width:50},
        {field: 'name', title: '学生姓名', minWidth:60, align:"center"},
        {field: 'sfz', title: '身份证', minWidth:80, align:'center'},
        {field: 'xjfh', title: '学籍辅号', minWidth:80, align:'center'},
        {field: 'xxmc', title: '学校名称', minWidth:80, align:'center'},
        {field: 'bjmc', title: '班级名称', minWidth:80, align:'center'},
        {title: '操作', minWidth:190,fixed:"right",align:"center",templet:"#studentListBar"}
        ]]
    });
    var currId,currType,currCurr=1,currLimit=10;
    $.post("/getXxxxBjxxTree", function (data) {
        tree.render({
            elem: '.treeStudentList'
            ,data: data//返回的数据
            ,click: function(obj){
                var tempId = obj.data.id;
                if(tempId.lastIndexOf("@@")==-1){
                    return false;
                }
                currType = tempId.split("@@")[0];
                currId = tempId.split("@@")[1];
                currCurr = 1;
                refreshStudentList(currId,currType,currCurr,currLimit);
            }
        });
    });
    function refreshStudentList(currId,currType,currCurr,currLimit){
        table.reload("studentListTable",{
            url:"/studentList",
            page: {
                page: currCurr
                ,limit: currLimit
            },
            where: {
                id: currId
                ,type: currType
                ,searchVal: $(".searchVal").val()  //搜索的关键字
            }
        })
    }
    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            currCurr = 1;
            refreshStudentList(currId,currType,currCurr,currLimit);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加学生
    function addStudent(edit){
        var index = layui.layer.open({
            title : "添加学生",
            type : 2,
            content : "/toStudentAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".name").val(edit.name);
                    body.find(".sfz").val(edit.sfz);
                    body.find(".xjfh").val(edit.xjfh);
                    body.find("#xsId").val(edit.xsId);
                    body.find("input[value='" + edit.sex + "']").attr("checked", "checked");
                    console.log(edit.xxxxId);
                    body.find("#xxxxId").val(edit.xxxxId);

                    var proHtml = '<option value="' + edit.bjxxId +  '" selected="selected">' + edit.bjmc + '</option>';
                    body.find('#bjxxId').html(proHtml);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回学生列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addStudent();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('studentListTable'),
            data = checkStatus.data,
            xsIds = "";
        if(data.length > 0) {
            for (var i in data) {
                if(i==data.length-1){
                    xsIds+=data[i].xsId;
                }else{
                    xsIds+=data[i].xsId+",";
                }
            }
            layer.confirm('确定删除选中的学生？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/studentDel",{
                    xsId : xsIds
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("删除失败！");
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的学生");
        }
    });

    function expStudent(){
        var index = layui.layer.open({
            title : "导入学生",
            type : 2,
            content : "/toStudentExp",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                setTimeout(function(){
                    layui.layer.tips('点击此处返回学生列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    //导入
    $(".expStu_btn").click(function(){
        expStudent();
    });

    //列表操作
    table.on('tool(studentList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addStudent(data);
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此学生？',{icon:3, title:'提示信息'},function(index){
                $.get("/studentDel",{
                    xsId : data.xsId //参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("删除失败！");
                    }
                })
            });
        }
    });
    document.onkeydown = function(e){
        var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            currCurr = 1;
            refreshStudentList(currId,currType,currCurr,currLimit);
        }
    }
})
